package com.lgj.springboot_lgj.web.controller;

import com.github.pagehelper.PageInfo;
import com.lgj.springboot_lgj.bean.basic.Article;
import com.lgj.springboot_lgj.bean.ext.ArticleExt;
import com.lgj.springboot_lgj.service.IArticleService;
import com.lgj.springboot_lgj.utils.EntityUtil;
import com.lgj.springboot_lgj.utils.Result;
import com.lgj.springboot_lgj.utils.ResultUtil;
import com.lgj.springboot_lgj.web.vm.ArticleVM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;

/**
 * @Author liguanghui
 * @Date 2022/6/19
 */
@Api(tags = "咨询信息模块")
@RestController
@RequestMapping("/auth/article")
public class ArticleController {
    @Autowired
    private IArticleService service;
    @ApiOperation("通过ID删除咨询信息")
    @ApiImplicitParam(name = "id",value = "编号",required = true,dataType = "int")
    //通过id删除
    @DeleteMapping("/deleteById")
    public Result deleteById(int id){
        service.deleteById(id);
        return ResultUtil.success("删除成功");
    }
    //通过id查询
    @ApiOperation("通过ID查询咨询信息")
    @ApiImplicitParam(name = "id",value = "编号",required = true,dataType = "int")
    @GetMapping("/findById")
    public Result findByIdWithCategoryAndUser(int id){
        ArticleExt articleExt = service.findByIdWithCategoryAndUser(id);
        return ResultUtil.success(articleExt);
    }
    //分页查询推荐咨询
    @ApiOperation("分页获取所有推荐的咨询信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码",required = true,dataType = "int"),
            @ApiImplicitParam(name = "pageSize",value = "每页大小",required = true,dataType = "int")
    })
    @GetMapping("/page/query/recommend")
    public Result fingRecommendAll(int pageNum, int pageSize){
        PageInfo<ArticleExt> pageInfo = service.pageQueryRecommend(pageNum,pageSize);
        return ResultUtil.success(pageInfo);
    }
    // 分页查询所有咨询
    @ApiOperation("分页查询所有的咨询信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码",required = true,dataType = "int"),
            @ApiImplicitParam(name = "pageSize",value = "每页大小",required = true,dataType = "int"),
            @ApiImplicitParam(name = "title",value = "标题支持模糊查询",dataType = "String"),
            @ApiImplicitParam(name = "status",value = "咨询状态0-待审核，1-不通过，2-通过，3-推荐",dataType = "int"),
            @ApiImplicitParam(name = "categoryId",value = "栏目id",dataType = "int"),
    })
    @GetMapping("/page/queryWithCategoryAndUser")
    public Result findArticleWithCategoryAndUser(int pageNum, int pageSize, String title, Integer status, Integer categoryId){
        PageInfo<ArticleExt> pageInfo = service.pageQueryArticleWithCategoryAndUser(pageNum,pageSize,title,status,categoryId);
        return ResultUtil.success(pageInfo);
    }

    //修改咨询状态
    @ApiOperation("修改咨询状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "编号",required = true,dataType = "int"),
            @ApiImplicitParam(name = "status",value = "状态,0-待审核，1-不通过，2-通过，3-推荐",required = true,dataType = "int")
    })
    @PutMapping("/changeStatus")
    public Result changeStatus(Integer id,Integer status){
        service.changeStatus(id,status);
        return ResultUtil.success("审核成功");
    }

    //保存或更新消息信息
    @ApiOperation("保存或更新消息信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleCategoryId",value = "所属栏目id",required = true,dataType = "int"),
            @ApiImplicitParam(name = "articleContent",value = "咨询内容",required = true,dataType = "String"),
            @ApiImplicitParam(name = "articleCover",value = "咨询封面地址",required = true,dataType = "String"),
            @ApiImplicitParam(name = "articleId",value = "编号",dataType = "int"),
            @ApiImplicitParam(name = "articlePublishTime",value = "咨询发布时间",required = true,dataType = "String"),
            @ApiImplicitParam(name = "articleReadTimes",value = "咨询阅读次数",required = true,dataType = "int"),
            @ApiImplicitParam(name = "articleStatus",value = "咨询状态",required = true,dataType = "int"),
            @ApiImplicitParam(name = "articleThumpUp",value = "咨询点赞数",required = true,dataType = "int"),
            @ApiImplicitParam(name = "articleTitle",value = "咨询题目",required = true,dataType = "String"),
            @ApiImplicitParam(name = "articleUserId",value = "咨询发布用户id",required = true,dataType = "int"),
    })
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(ArticleVM articlevm){
        service.saveOrUpdate(EntityUtil.vm2Entity(articlevm,Article.class));
        return ResultUtil.success("操作成功");
    }
}
